﻿using System;
using System.ComponentModel;
using System.Text;

public abstract class BaseGenerator
{
    private readonly StringBuilder _builder = new StringBuilder();
    private string _namespace;

    public abstract string Generate();

    public override string ToString()
    {
        return _builder.ToString();
    }

    public string Namespace
    {
        get { return _namespace; }
        set { _namespace = value; }
    }

    protected void Write(object obj)
    {
        _builder.Append(obj ?? string.Empty);
    }

    protected void Write(object obj, params object[] args)
    {
        _builder.AppendFormat((obj ?? string.Empty).ToString(), args);
    }

    protected void WriteLine(object obj, params object[] args)
    {
        Write(obj, args);
        _builder.AppendLine();
    }

    protected static T FromString<T>(string str)
    {
        Type type = typeof(T);
        if (type == typeof(string))
            return (T)(object)str;
        TypeConverter converter = TypeDescriptor.GetConverter(type);
        if (converter == null || !converter.CanConvertFrom(typeof(string)))
            return default(T);
        return (T)converter.ConvertFromString(str);
    }
}